<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="采购单"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <purchase ref="purchase"></purchase>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>

<script>
import VolBox from "@/components/basic/VolBox.vue";
import purchase from "@/views/purchaseinfo/purchase/q_purchase.vue";
export default {
  components: {
    "vol-box": VolBox,
    purchase,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
      this.$refs.purchase.searchFormFields.AuditStatus = "1";
      this.$refs.purchase.searchFormFields.PaymentState = "0";
      this.$refs.purchase.searchFormFields.Voucher_sub = null;
      this.$refs.purchase.searchFormFields.Supplier = null;
      this.$refs.purchase.searchFormFields.Contract_intro = null;
    },
    open() {
      // let setIntervalTiem = setInterval(() => {
      //   if (this.$refs.purchase) {
      //     clearInterval(setIntervalTiem);
      //     this.$refs.purchase.$refs.grid.search();
      //   }
      // }, 100);
      this.model = true;
    },
    async detailOnSelect() {
     try{
      let rowObj = this.$refs.purchase.$refs.grid.getSelectRows();
      if (!rowObj.length) return this.base.ElMessageFn("请至少选择一条采购单");
      let list = await rowObj.filter(
        (item) => item.AuditStatus == 1 && item.PaymentState == 0
      ); //审核通过&&未采购
      let list2 = await rowObj.filter((item) => item.AuditStatus != 1); //非审核通过
      let list3 = await rowObj.filter((item) => item.PaymentState != 0); //非未付款单据
      if (list2.length) {
        let str = "";
        list2.forEach((r) => {
          if (str == "") str += r.Voucher_sub;
          else str = str + "," + r.Voucher_sub;
        });
        this.$message.warning(`单据${str}未审核通过`);
      }
      if (list3.length) {
        let str = "";
        list3.forEach((r) => {
          if (str == "") str += r.Voucher_sub;
          else str = str + "," + r.Voucher_sub;
        });
        this.$message.warning(`${str}非未付款单据`);
      }
      this.$emit("parentCall", ($parent) => {
        let parentList = $parent.$refs.detail.rowData;
        let set = new Set(parentList.map((item) => item.Voucher_sub)); //得到由明细表goods_sub组成的数组
        let commonObjects = list.filter((item) => set.has(item.Voucher_sub)); //得到明细表中已包含的选中行
        let goodsNamesSet = new Set(
          commonObjects.map((item) => item.Voucher_sub)
        ); //相同商品的编码
        if (commonObjects.length) {
          list = list.filter((item) => !goodsNamesSet.has(item.Voucher_sub)); //在选中数据中剔除明细表已包含的行数据
          let str = "";
          commonObjects.forEach((r) => {
            if (str == "") str += r.Voucher_sub;
            else str = str + "," + r.Voucher_sub;
          });
          this.$message.warning(`采购单${str}已存在此付款单`);
        }

        let arr = list.map((item) => {
          return {
            Voucher_sub: item.Voucher_sub,
            Goods_num: item.Goods_num,
            Total_price: item.Total_price,
            Paid_price: item.Total_price,
            Supplier: item.Supplier,
            PaymentType: item.PaymentType,
          };
        });
        $parent.$refs.detail.rowData.unshift(...arr);
        if(arr.length&&this.$route.path == "/q_purchase_payment"){
          $parent.detailFn()
        }
        this.onModelClose();
        return (this.model = false);
      });
     }catch (e){
      console.log(e)
     }
    },
  },
};
</script>

<style>
</style>